import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "@/store";

Vue.use(VueRouter)

const routes = [
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/Login.vue')
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import('../views/Register.vue')
    },
    {
        path: '/forget',
        name: 'forget',
        component: () => import('../views/forget.vue')
    },
    {
        path: '/404',
        name: '404',
        component: () => import('../views/404.vue')
    },
    //前台路由
    {
        path: '/front',
        name: 'Front',
        component: () => import('../views/front/Front.vue'),
        children: [
            {
                path: 'home',
                name: 'FrontHome',
                component: () => import('../views/front/Home.vue')
            },
            {
                path: 'order',
                name: 'Order',
                component: () => import('../views/front/order.vue')
            },
            {
                path: 'preOrder',
                name: 'preOrder',
                component: () => import('../views/front/preOrder.vue')
            },
            {
                path: 'comments',
                name: 'comments',
                component: () => import('../views/front/comments.vue')
            },
            {
                path: 'mydingzhi',
                name: 'mydingzhi',
                component: () => import('../views/front/mydingzhi.vue')
            },
            //个人中心路由
            {
                path: 'center',
                name: 'Center',
                component: () => import('../views/front/Center.vue'),
                children: [{
                    path: 'person',
                    name: 'CenterFrontPerson',
                    component: () => import('../views/front/Person.vue')
                }, {
                    path: 'shoppingcart',
                    name: 'CenterShoppingCart',
                    component: () => import('../views/front/ShoppingCart.vue')
                }, {
                    path: 'address',
                    name: 'CenterAddress',
                    component: () => import('../views/front/Address.vue')
                }, {
                    path: 'password',
                    name: 'CenterFrontPassword',
                    component: () => import('../views/front/Password.vue')
                },
                    {
                        path: 'order',
                        name: 'centerOrder',
                        component: () => import('../views/front/order.vue')
                    },
                ]
            },
            {
                path: 'address',
                name: 'Address',
                component: () => import('../views/front/Address.vue')
            },
            {
                path: 'person',
                name: 'FrontPerson',
                component: () => import('../views/front/Person.vue')
            },
            {
                path: 'password',
                name: 'FrontPassword',
                component: () => import('../views/front/Password.vue')
            },

            {
                path: 'dingzhi',
                name: 'DingZhi',
                component: () => import('../views/front/DingZhi.vue')
            },
            {
                path: 'seckill',
                name: 'Seckill',
                component: () => import('../views/front/Seckill.vue')
            },
            {
                path: 'message',
                name: 'Message',
                component: () => import('../views/front/Message.vue')
            },
            {
                path: 'cart',
                name: 'Cart',
                component: () => import('../views/front/cart.vue')
            }, {
                path: 'shoppingcart',
                name: 'shoppingCart',
                component: () => import('../views/front/ShoppingCart.vue')
            },
            {
                path: 'buy',
                name: 'Buy',
                component: () => import('../views/front/Buy.vue')
            },
            {
                path: 'skillbuy',
                name: 'skillBuy',
                component: () => import('../views/front/SkillBuy.vue')
            },
        ]
    },
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

//重置路由
export const resetRouter = () => {
    router.matcher = new VueRouter({
        mode: 'history',
        base: process.env.BASE_URL,
        routes
    })
}

// 刷新页面会导致页面路由重置
export const setRoutes = () => {
    const storeMenus = localStorage.getItem("menus");

}

// 刷新再set一次路由
setRoutes()


router.beforeEach((to, from, next) => {
    localStorage.setItem("currentPathName", to.name)  // 设置当前的路由名称
    store.commit("setPath")
    // 未找到路由的情况
    if (!to.matched.length) {
        const storeMenus = localStorage.getItem("menus")
        if (storeMenus) {
            next("/404")
        } else {
            // 跳回home页面
            next("/front/home")
        }
    }
    // 其他的情况都放行
    else {
        next()
    }

})

export default router
